//
//  MyUITableViewController.m
//  MoCard
//
//  Created by 水石 温 on 11-11-4.
//  Copyright 2011年 __MyCompanyName__. All rights reserved.
//

#import "MyUITableViewController.h"
#import "AppDelegate.h"
#import "Global.h"
#define UIBarButtonItem_width  44
#define UIBarButtonItem_height 33

@implementation MyUITableViewController
@synthesize tableView;
- (id)initWithStyle:(UITableViewStyle)style{
    self = [super init];
    if (self) {
        tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-NAVI_BAR_HEIGHT) style:style];
    }
    return self;
}
//-(void)viewWillAppear:(BOOL)animated {
//    [super viewWillAppear:animated];
//    [self.navigationController setNavigationBarHidden:YES animated:animated];
//}

+(UIBarButtonItem*)backBarButtonItemWithTarget:(id)target action:(SEL)selector {
    //48*31 10
    UIButton *btnView=[UIButton buttonWithType:UIButtonTypeCustom];
    [btnView setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [btnView setTitle:@"返回" forState:UIControlStateNormal];
    btnView.frame=CGRectMake(0, 0, UIBarButtonItem_width, UIBarButtonItem_height);
    UIBarButtonItem *barButtonItem=[[UIBarButtonItem alloc] initWithCustomView:btnView];
    [btnView addTarget:target action:selector forControlEvents:UIControlEventTouchUpInside];
    return barButtonItem;
    
}
+(UIBarButtonItem*)imageBarButtonItem:(UIImage*)image target:(id)target action:(SEL)selector {
    UIButton *btnView=[UIButton buttonWithType:UIButtonTypeCustom];
    btnView.frame = CGRectMake(0, 0, UIBarButtonItem_width, UIBarButtonItem_width);
    [btnView setImage:image forState:UIControlStateNormal];
    UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:btnView];
    [btnView addTarget:target action:selector forControlEvents:UIControlEventTouchUpInside];
    return barButtonItem;
}
+(UIBarButtonItem*)leftMenuBarButtonItem{
    AppDelegate *appDele = (AppDelegate*)[UIApplication sharedApplication].delegate;
    UIBarButtonItem *leftItem = [MyUITableViewController imageBarButtonItem:[UIImage imageNamed:@"home_push_menu.png"] target:appDele.rootViewController action:@selector(showLeftView)];
    return leftItem;
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                                                     [UIColor whiteColor], UITextAttributeTextColor,
                                                                     nil]];
    
    UINavigationBar *navBar = [self.navigationController navigationBar];
    if ([navBar respondsToSelector:@selector(setBackgroundImage:forBarMetrics:)]) {
        if (IOS7_OR_LATER) {
            [navBar setBackgroundImage:IMG(@"navi_bar_iphone5.png") forBarMetrics:0];
        }else{
            [navBar setBackgroundImage:IMG(@"navi_bar.png") forBarMetrics:0];
        }
    }
    [Global SetSubViewExternNone:self];
    
    self.view.backgroundColor = RGB(255, 255, 255, 1);
    
    if (tableView == nil) {
        tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-NAVI_BAR_HEIGHT-20)];
    }
    tableView.dataSource = self;
    tableView.delegate = self;
    tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    tableView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:tableView];
    UIView *footView = [[UIView alloc] initWithFrame:RECT(0, 0, SCREEN_WIDTH, 0)];
    tableView.tableFooterView = footView;
    
    NSInteger vcCount = [self.navigationController.viewControllers count];
//    if (vcCount>1) {
//        self.navigationItem.leftBarButtonItem=[self backBarButtonItemWithTarget:self action:@selector(back:)];
//    }
//    else
        if (vcCount==1) {
        UIBarButtonItem *leftItem = [MyUITableViewController leftMenuBarButtonItem];
        self.navigationItem.leftBarButtonItem=leftItem;
    }
}

-(void)setTitleByImgName:(NSString *)titleImgName{
    UIImage *image = IMG(titleImgName);
    UIImageView *titleImageView = [[UIImageView alloc] initWithFrame:CGRectMake((FW(naviView)-image.size.width)/2, 17, image.size.width, image.size.height)];
    titleImageView.image = IMG(titleImgName);
    [naviView addSubview:titleImageView];
}
- (void)dealloc
{
    [NC removeObserver:self];
}

-(void)back:(id)sender {
    if ([self.navigationController.viewControllers count]>1) {
        [self.navigationController popViewControllerAnimated:YES];
    } else {
        [self.navigationController dismissModalViewControllerAnimated:YES];
    }
}



@end
